package com.kfc.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.kfc.dao.UserDao;
import com.kfc.vo.Customer;

@Repository("userDao")
public class UserDaoImp implements UserDao{
	
	@Autowired(required=true)
	private BasicDataSource bds;
	
	private Connection conn;
	private PreparedStatement ps;
	private ResultSet rs;
	
	@Override
	public boolean insertUser(Customer s) {
		
		String sql = "insert into student (st_username,st_passwd,st_name,st_id,st_sex) values (?,?,?,?,?)";
		try {
			conn = bds.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setString(1, s.getSt_username());
			ps.setString(2, s.getSt_passwd());
			ps.setString(3, s.getSt_name());
			ps.setString(4, s.getSt_id());
			ps.setString(5, s.getSt_sex());
			int result = ps.executeUpdate();
			if (result < 1) {
				return false;
			}else {
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
	}

	@Override
	public void getUserByUsernameAndPasswd(Customer user) {
		String sql = "select * from student where st_username= ? and st_passwd = ?";
		
		try {
			conn = bds.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setString( 1 , user.getSt_username());
			ps.setString(2, user.getSt_passwd());
			rs = ps.executeQuery();
			while(rs.next()) {
				user.setSt_id(rs.getString("st_id"));
				user.setSt_name(rs.getString("st_name"));
				user.setSt_sex(rs.getString("st_sex"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	
	
}
